<?php
/*
 *  Maniter: A webbased business managment tool.
 *  Copyright (C) 2007  Dimitri Michaux <dimitri.michaux@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * templateengine.class.php, Maniter
 * 
 * This class is suppossed to represent a templateengine, but it doesn't work
 * the way it's supposed and has to be completely rewritten.
 *
 * @author Dimitri Michaux
 * @version 0.1
 * @package maniter_classes
 */

class TemplateEngine{
	
	private $_templates;
	private $_tabs = array();
	private $_title;
	private $_javaScript = array();
	private $_formOpen;
	private $_formClose;
	
	/**
	 * Buttons to be shown
	 * s for save and r for refresh
	 * 
	 * @var String
	 */
	private $_buttons;
	
	public function __construct(){
		$this->_buttons = '';
		$templatePath = "templates/";
		
		//templates
		//main
		$this->_templates['redirect'] 	= $templatePath."redirect.tpl";
		$this->_templates['header'] 	= $templatePath."header.tpl";
		$this->_templates['footer'] 	= $templatePath."footer.tpl";
		
		//tabs
		$this->_templates['tab-headers'] 	= $templatePath."tab-headers.tpl";
		$this->_templates['tab-contents']	= $templatePath."tab-contents.tpl";
		
		//editing
		$this->_templates['edit-form-open'] 		= $templatePath."edit-form-open.tpl";
		$this->_templates['edit-form-close'] 		= $templatePath."edit-form-close.tpl";
		$this->_templates['edit-form-client'] 		= $templatePath."edit-form-client.tpl";
		$this->_templates['edit-form-purchases']	= $templatePath."edit-form-purchases.tpl";
		$this->_templates['edit-form-text'] 		= $templatePath."edit-form-text.tpl";
		
		//settings
		$this->_templates['settings-form-open'] 	= $templatePath."settings-form-open.tpl";
		$this->_templates['settings-form-close'] 	= $templatePath."settings-form-close.tpl";
		$this->_templates['settings-form-offer'] 	= $templatePath."settings-form-offer.tpl";
		$this->_templates['settings-form-order'] 	= $templatePath."settings-form-order.tpl";
		$this->_templates['settings-form-company']	= $templatePath."settings-form-company.tpl";
	}
	
	public function addTab( $title, $content, $active=false ){
		$this->_tabs[] = new Tab( $title, $content, $active );
	}
	
	public function addJavaScript( $script ){
		$this->_javaScript[] = $script;
	}
	
	public function setTitle( $title ){
		$this->_title = $title;
	}
	
	public function setForm( $open, $close ){
		$this->_formOpen = $open;
		$this->_formClose = $close;
	}
	
	public function editPage( &$order ){
		$this->_buttons = 'sr';
		
		//form
		ob_start();
		include( $this->_templates['edit-form-open'] );
		$formOpen = ob_get_contents();
		ob_clean();
		
		include( $this->_templates['edit-form-close'] );
		$formClose = ob_get_contents();
		ob_clean();
		
		$this->setForm( $formOpen, $formClose );
		
		//build tabs
		include($this->_templates['edit-form-purchases']);
		$this->addTab( "Producten", ob_get_contents() );
		ob_clean();
		
		include($this->_templates['edit-form-client']);
		$this->addTab( "Klant", ob_get_contents() );
		ob_clean();
		
		include($this->_templates['edit-form-text']);
		$this->addTab( "Teksten", ob_get_contents() );
		ob_end_clean();
	}
	
	public function settingsPage( &$ownerInfo ){
		global $_DB;
		
		$this->_buttons = 's';
		
		//forms
		ob_start();
		include( $this->_templates['settings-form-open'] );
		$formOpen = ob_get_contents();
		ob_clean();
		
		include( $this->_templates['settings-form-close'] );
		$formClose = ob_get_contents();
		ob_clean();
		
		$this->setForm( $formOpen, $formClose );
			
		//company information
		include( $this->_templates['settings-form-company'] );
		$this->addTab( 'Bedrijfs informatie', ob_get_contents() );
		ob_clean();
		
		//
		include( $this->_templates['settings-form-order'] );
		$this->addTab( 'Factuur', ob_get_contents() );
		ob_clean();
		
		include( $this->_templates['settings-form-offer'] );
		$this->addTab( 'Offerte', ob_get_contents() );
		ob_end_clean();
	}
	
  public function calendarPage( $month=0, $day=0,$year=0, $edit=0 )
  {
    if( $month==0 )
      $month = date('m', time() );
      
    if( $year==0 )
      $year  = date('Y', time() );
      
    if( $day==0 )
      $day   = date('d', time() );
    
    $this->addJavaScript( "java/calendar.js" );
    
    ob_start();
  
    $this->sendHeader();
    $title =& $this->_title;
    $javaScript =& $this->_javaScript;
    
    include( $this->_templates['header'] );
    
    include( "templates/calendar.tpl" );
    
    include( $this->_templates['footer'] );
    
    ob_end_flush();
  }
	
	public function output(){
		ob_start();
		
		$this->sendHeader();
		$title =& $this->_title;
		$javaScript =& $this->_javaScript;
		include( $this->_templates['header'] );
		
		$formOpen =& $this->_formOpen;
		$formClose =& $this->_formClose;
		$buttons =& $this->_buttons;
		$tabs =& $this->_tabs;
		include( $this->_templates['tab-contents'] );
		
		include( $this->_templates['footer'] );
		
		ob_end_flush();
	}
	
	public function redirect( $page ){
		$this->sendHeader();
		include( $this->_templates['redirect'] );
	}
	
	private function sendHeader(){
		header('Content-Type: text/html; charset=ISO-8859-1');
	}
}

class Tab{
	public $title;
	public $content;
	public $active;
	
	public function __construct( $title, $content, $active=false ){
		$this->content = $content;
		$this->title = $title;
		$this->active = $active;
	}
}

?>